package com.akk.module.admin.service;

import com.akk.module.admin.entity.*;
import com.akk.module.admin.mapper.AdminRoleMapper;
import com.akk.module.admin.mapper.AdminRoleMenuMapper;
import com.akk.module.admin.mapper.dao.AdminRoleDao;
import com.akk.module.admin.mapper.dao.AdminRoleMenuDao;
import com.akk.module.admin.model.response.AdminRoleRes;
import com.akk.utils.IDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class AdminRoleService {
    @Autowired
    AdminRoleMapper adminRoleMapper;
    @Autowired
    AdminRoleDao adminRoleDao;
    @Autowired
    AdminRoleMenuDao adminRoleMenuDao;
    @Autowired
    AdminRoleMenuMapper adminRoleMenuMapper;
    @Autowired
    AdminRoleMenuExample adminRoleMenuExample;
    @Autowired
    AdminRoleExample adminRoleExample;

    public List<AdminRoleRes> findByParam(Map param) {

        AdminRole role = new AdminRole();
        if(param.containsKey("fromAdminId")){
            role.setFromAdminId(param.get("fromAdminId").toString());
        }
        return adminRoleDao.selectAdminRoleList(role);
    }



    public void save(AdminRole role, List<AdminMenu> menuList) {
        adminRoleMapper.insert(role);
        List<AdminRoleMenu> roleMenus = new ArrayList<>();
        for(AdminMenu menu:menuList){
            AdminRoleMenu roleMenu = new AdminRoleMenu();
            roleMenu.setRoleMenuId(IDUtils.getPK());
            roleMenu.setMenuId(menu.getMenuId());
            roleMenu.setRoleId(role.getRoleId());
            roleMenus.add(roleMenu);
        }
        adminRoleMenuDao.insertBatch(roleMenus);
    }

    public AdminRole findByRoleId(String roleId) {
        return adminRoleMapper.selectByPrimaryKey(roleId);
    }

    public void deleteByRoleId(String roleId) {
        adminRoleMapper.deleteByPrimaryKey(roleId);
    }

    public List<AdminRoleMenu> findRoleMenuByRoleId(String roleId) {
        adminRoleMenuExample.clear();
        adminRoleMenuExample.createCriteria().andRoleIdEqualTo(roleId);
        return adminRoleMenuMapper.selectByExample(adminRoleMenuExample);
    }

    public void saveRoleMenu(AdminRoleMenu roleMenu) {
        adminRoleMenuMapper.insert(roleMenu);
    }

    public void deleteByRoleIdAndMenuId(String roleId, String menuId) {
        adminRoleMenuExample.clear();
        adminRoleMenuExample.createCriteria().andRoleIdEqualTo(roleId).andMenuIdEqualTo(menuId);
        adminRoleMenuMapper.deleteByExample(adminRoleMenuExample);
    }

    public void update(AdminRole role) {
        adminRoleMapper.updateByPrimaryKey(role);
    }

    public List<AdminRole> findByRoleType(Integer roleType) {
        adminRoleExample.clear();
        adminRoleExample.createCriteria().andRoleTypeEqualTo(roleType);
        return adminRoleMapper.selectByExample(adminRoleExample);
    }

    public List<AdminRole> findByRoleTypeOrAppId(Integer roleType, String appId) {
        adminRoleExample.clear();
        AdminRoleExample.Criteria criteria1 = adminRoleExample.createCriteria();
        criteria1.andRoleTypeEqualTo(roleType);
        AdminRoleExample.Criteria criteria2 = adminRoleExample.createCriteria();
        criteria2.andAppIdEqualTo(appId);
        adminRoleExample.or(criteria2);

        return adminRoleMapper.selectByExample(adminRoleExample);
    }

    public List<AdminRole> findByRoleTypeAndAppIdIsNullAndFromAdminId(Integer roleType,String adminId) {
        adminRoleExample.clear();
        adminRoleExample.createCriteria().andAppIdIsNull().andRoleTypeEqualTo(roleType).andFromAdminIdEqualTo(adminId);
        return adminRoleMapper.selectByExample(adminRoleExample);
    }

    public List<AdminRole> findByAppIdAndFromAdminId(String appId,String adminId) {
        adminRoleExample.clear();
        adminRoleExample.createCriteria().andAppIdEqualTo(appId).andFromAdminIdEqualTo(adminId);
        return adminRoleMapper.selectByExample(adminRoleExample);
    }
}
